#include "../include/e_fps_camera.h"

#include <iostream>
using namespace std;

e_fps_camera::e_fps_camera()
{
    look_vector.set(0.0f, 0.0f, -1.0f);
    up_vector.set(0.0f, 1.0f, 0.0f);
    right_vector.set(1.0f, 0.0f, 0.0f);
    posision.x = 0;
    posision.y = 0;
    posision.z = 0;

    right_now = 0;
    left_now = 0;
    fowards_now = 0;
    backwards_now = 0;

    SDL_ShowCursor(0);
    SDL_WM_GrabInput(SDL_GRAB_ON);
    camera_x = 0;
    camera_y = 0;
}

void e_fps_camera::update()
{

    /*glRotatef(0 - delta_x, up_vector.x, up_vector.y, up_vector.z);
    camera_x -= delta_x;
    delta_x = 0;
    right_vector.x = cos(camera_x*(M_PI/180));
    right_vector.z = sin(camera_x*(M_PI/180));
    look_vector.x = cos((camera_x+90)*(M_PI/180));
    look_vector.z = sin((camera_x+90)*(M_PI/180));

    glTranslatef(right_vector.x*strafe_amount, right_vector.y*strafe_amount, right_vector.z*strafe_amount);
    glTranslatef(look_vector.x*fowards_amount, look_vector.y*fowards_amount, look_vector.z*fowards_amount);*/

    glTranslatef(0 - posision.x, 0 - posision.y, 0 - posision.z);
    glRotatef((float)camera_x*0.1, 0.0f, 1.0f, 0.0f);
    //glRotatef((float)camera_y, cos((float)camera_x*(M_PI/180.0)), sin((float)camera_x*(M_PI/180.0)), 0.0f);

}

void e_fps_camera::handle_input(SDL_Event *event, int time_passed)
{
    if( event->type == SDL_KEYDOWN )
    {
        switch( event->key.keysym.sym )
        {
            case SDLK_UP: fowards_now = -1; break;
            case SDLK_DOWN: backwards_now = 1; break;
            case SDLK_LEFT: left_now = 1; break;
            case SDLK_RIGHT: right_now = -1; break;
            case SDLK_ESCAPE: SDL_Quit();
            default: break;
        }
    }
    else if( event->type == SDL_KEYUP )
    {
        switch( event->key.keysym.sym )
        {
            case SDLK_UP: fowards_now = 0; break;
            case SDLK_DOWN: backwards_now = 0; break;
            case SDLK_LEFT: left_now = 0; break;
            case SDLK_RIGHT: right_now = 0; break;
            default: break;
        }
    }
    else if( event->type == SDL_MOUSEMOTION )
    {
        SDL_GetMouseState(&mouse_x, &mouse_y);
        if(mouse_x != 320) camera_x -= 320 - mouse_x;
        if(mouse_y != 240) camera_y += 240 - mouse_y;
        SDL_WarpMouse(320, 240);

    }

    fowards_amount = .01 * (fowards_now + backwards_now) * (float)time_passed;
    strafe_amount = .01 * (right_now + left_now) * (float)time_passed;
}
